//
//  DB.m
//  UI_数据库
//
//  Created by dllo on 15/5/11.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "DB.h"

//确保数据库指针不管创建多少次, 永远都是同一个指针
static sqlite3 *dbPoint = nil;
@implementation DB
+ (sqlite3 *)openDB
{
    if (dbPoint) {
        return dbPoint;
    }
    //判断library中是否有数据库文件, 如果没有则拷贝进去,如果有则什么都不做,
    NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/shici.db", path];
    
//    NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
//    NSString *path1 = [array1 lastObject];
    NSString *path1 = [[NSBundle mainBundle]pathForResource:@"shici" ofType:@"db"];
    NSLog(@"%@", path);
    BOOL judge = [manager fileExistsAtPath:path];
    if (!judge) {
       [manager copyItemAtPath:path1 toPath:path error:nil];
        
    }
    //获得拷贝到的数据库文件的指针
    //打开数据库,并获得数据库指针
    sqlite3_open([path UTF8String], &dbPoint);
    return dbPoint;
}

+ (void)closeDB
{
    sqlite3_close(dbPoint);
}
@end
